package com.dhnsoft.代码随想录.链表;

import java.util.List;
import java.util.Stack;

/**
 * ClassName: d03_206_反转链表
 * Package: com.dhnsoft.代码随想录.链表
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/8 11:27
 * @Version 1.0
 */
public class d03_206_反转链表 {
    public static void main(String[] args) {
        LinkedNode singLinked = new LinkedNode();
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        singLinked.add(l1);
        singLinked.add(l2);
        singLinked.add(l3);
        singLinked.add(l4);
        ListNode listNode = reverseList(singLinked.getHead());
        System.out.println(listNode);

    }

    public static ListNode reverseList(ListNode head) {
        // 边缘条件判断
        if(head == null) return null;
        if (head.next == null) return head;
        ListNode list = new ListNode(-1);
        list.next = head;
        ListNode cur = list.next;
        ListNode next =null;
        ListNode newList = new ListNode(-1);
        while (cur!=null){
            next = cur.next;
            cur.next = newList.next;
            newList.next = cur;
            cur = next;
        }
        head = newList.next;
        return head;


    }

}
